package com.ruoyi.information.controller.back;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.information.domain.back.ServiceCenterBack;
import com.ruoyi.information.service.back.IServiceCenterBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 体系分类Controller
 *
 * @author ruoyi
 * @date 2023-05-04
 */
@RestController
@RequestMapping("/centerBack")
public class ServiceCenterBackController extends BaseController {
    @Autowired
    private IServiceCenterBackService serviceCenterService;

    /**
     * 查询体系分类列表
     */
    @RequiresPermissions("information:center:list")
    @GetMapping("/list")
    public AjaxResult list(ServiceCenterBack serviceCenter) {
        List<ServiceCenterBack> list = serviceCenterService.selectServiceCenterList(serviceCenter);
        return success(list);
    }

    /**
     * 导出体系分类列表
     */
    @RequiresPermissions("information:center:export")
    @Log(title = "体系分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ServiceCenterBack serviceCenter) {
        List<ServiceCenterBack> list = serviceCenterService.selectServiceCenterList(serviceCenter);


        //筛选出根节点
        List<ServiceCenterBack> listAll = list.stream().filter(menu -> menu.getParentId() == 0).peek(
                //设置子节点信息
                menu -> menu.setCenterBackList(getChildrens(menu, list))
        ).collect(Collectors.toList());


        List<ServiceCenterBack> finalList = new ArrayList<>();
        //一层
        for (ServiceCenterBack a1 : listAll) {
            if (a1.getCenterBackList() != null && !a1.getCenterBackList().isEmpty()) {
                //二层
                for (ServiceCenterBack a2 : a1.getCenterBackList()) {
                    if (a2.getCenterBackList() != null && !a2.getCenterBackList().isEmpty()) {
                        //三层
                        for (ServiceCenterBack a3 : a2.getCenterBackList()) {
                            if (a3.getCenterBackList() != null && !a3.getCenterBackList().isEmpty()) {
                                //四层
                                for (ServiceCenterBack a4 : a3.getCenterBackList()) {
                                    if (a4.getCenterBackList() != null && !a4.getCenterBackList().isEmpty()) {
                                        //五层
                                        for (ServiceCenterBack a5 : a4.getCenterBackList()) {
                                            if (a5.getCenterBackList() != null && !a5.getCenterBackList().isEmpty()) {
                                                //六层
                                                for (ServiceCenterBack a6 : a5.getCenterBackList()) {
                                                    ServiceCenterBack back = new ServiceCenterBack();
                                                    back.setServiceName1(a1.getServiceName());
                                                    back.setServiceName2(a2.getServiceName());
                                                    back.setServiceName3(a3.getServiceName());
                                                    back.setServiceName4(a4.getServiceName());
                                                    back.setServiceName5(a5.getServiceName());
                                                    back.setServiceName6(a6.getServiceName());
                                                    finalList.add(back);
                                                }

                                            } else {
                                                ServiceCenterBack back = new ServiceCenterBack();
                                                back.setServiceName1(a1.getServiceName());
                                                back.setServiceName2(a2.getServiceName());
                                                back.setServiceName3(a3.getServiceName());
                                                back.setServiceName4(a4.getServiceName());
                                                back.setServiceName5(a5.getServiceName());
                                                finalList.add(back);
                                            }
                                        }
                                    } else {
                                        ServiceCenterBack back = new ServiceCenterBack();
                                        back.setServiceName1(a1.getServiceName());
                                        back.setServiceName2(a2.getServiceName());
                                        back.setServiceName3(a3.getServiceName());
                                        back.setServiceName4(a4.getServiceName());
                                        finalList.add(back);
                                    }
                                }
                            } else {
                                ServiceCenterBack back = new ServiceCenterBack();
                                back.setServiceName1(a1.getServiceName());
                                back.setServiceName2(a2.getServiceName());
                                back.setServiceName3(a3.getServiceName());
                                finalList.add(back);
                            }
                        }
                    } else {
                        ServiceCenterBack back = new ServiceCenterBack();
                        back.setServiceName1(a1.getServiceName());
                        back.setServiceName2(a2.getServiceName());
                        finalList.add(back);
                    }
                }
            } else {
                ServiceCenterBack back = new ServiceCenterBack();
                back.setServiceName1(a1.getServiceName());
                finalList.add(back);
            }
        }


        ExcelUtil<ServiceCenterBack> util = new ExcelUtil<ServiceCenterBack>(ServiceCenterBack.class);
        util.exportExcel(response, finalList, "体系分类数据");
    }

    private List<ServiceCenterBack> getChildrens(ServiceCenterBack root, List<ServiceCenterBack> menus) {
        List<ServiceCenterBack> list = menus.stream().filter(menu ->
                //筛选出下一节点元素
                Objects.equals(menu.getParentId(), root.getId())).peek(menu -> {
            //递归set子节点
            menu.setCenterBackList(this.getChildrens(menu, menus));
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * 获取体系分类详细信息
     */
    @RequiresPermissions("information:center:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(serviceCenterService.selectServiceCenterById(id));
    }

    /**
     * 新增体系分类
     */
    @RequiresPermissions("information:center:add")
    @Log(title = "体系分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ServiceCenterBack serviceCenter) {
        return toAjax(serviceCenterService.insertServiceCenter(serviceCenter));
    }

    /**
     * 修改体系分类
     */
    @RequiresPermissions("information:center:edit")
    @Log(title = "体系分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ServiceCenterBack serviceCenter) {
        return toAjax(serviceCenterService.updateServiceCenter(serviceCenter));
    }

    /**
     * 删除体系分类
     */
    @RequiresPermissions("information:center:remove")
    @Log(title = "体系分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(serviceCenterService.deleteServiceCenterByIds(ids));
    }
}
